# -*- coding: utf-8 -*-
import random
from pathlib import Path
import codecs
import itchat
import datetime


def send_room_msg(chat, name, msg):
    to_user_name = chat.search_chatrooms(name=name)[0]['UserName']
    chat.send(msg, toUserName=to_user_name)


def send_user_msg(chat, name, msg):
    to_user_name = chat.search_friends(name=name)[0]['UserName']
    chat.send(msg, toUserName=to_user_name)


def get_one_msg():
    msg_list = []
    for e in Path('./book').glob('*.txt'):
        with codecs.open(str(e), encoding='utf-8') as fd:
            msg_list.extend(map(lambda x: '#'.join([e.stem, x]), fd.readlines()))
    return random.choice(msg_list)


if __name__ == '__main__':
    message = get_one_msg()
    print(message)
    itchat.auto_login(hotReload=True)
    send_room_msg(itchat, '欢乐西安', message)
    print('\t\t------打卡成功', datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
